package com.wantupai.app.main;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.wantupai.app.R;
import com.wantupai.app.base.BaseActivity;
import com.wantupai.app.camera.CameraActivity;
import com.wantupai.app.main.fragment.FindFragment;
import com.wantupai.app.main.fragment.PersonalFragment;


/**
 * @author wmq
 * @date 2019-05-13
 * @description mainActivity
 */
public class MainActivity extends BaseActivity {

    private static final int FINDFRAGMENTTAG = 1;
    private static final int PERSONFRAGMENTTAG = 2;

    RadioGroup radioGroup;
    RadioButton rbLeft;
    RadioButton rbRight;
    ImageView btCenter;
    FrameLayout fragmentBox;


    /**
     * 发现
     */
    private FindFragment mFindFragment;
    /**
     * 我的
     */
    private PersonalFragment mPersonalFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initViews() {
        fragmentBox = findViewById(R.id.fl_box);
        radioGroup = findViewById(R.id.radio_group);
        rbLeft = findViewById(R.id.rb_left);
        rbRight = findViewById(R.id.rb_right);
        btCenter = findViewById(R.id.bt_center);
        radioGroup.check(R.id.rb_left);
        changeFragment(FINDFRAGMENTTAG);
    }

    @Override
    protected boolean translucentStatusBar() {
        return true;
    }

    @Override
    public void initListener() {
        setOnClick(rbLeft);
        setOnClick(rbRight);
        setOnClick(btCenter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
            case R.id.rb_left://发现
                radioGroup.check(R.id.rb_left);
                changeFragment(FINDFRAGMENTTAG);
                break;
            case R.id.rb_right://我的
                radioGroup.check(R.id.rb_right);
                changeFragment(PERSONFRAGMENTTAG);
                break;
            case R.id.bt_center://识别
                startActivity(CameraActivity.class, false);
                break;
        }
    }

    private void changeFragment(int FRAGMENTTAG) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFfragment(fragmentTransaction);
        showFragment(fragmentTransaction, FRAGMENTTAG);
        fragmentTransaction.commit();
    }

    public void showFragment(FragmentTransaction transaction, int index) {
        if (FINDFRAGMENTTAG == index) {
            if (mFindFragment != null) {
                transaction.show(mFindFragment);
            } else {
                mFindFragment = FindFragment.getInstance();
                transaction.add(R.id.fl_box, mFindFragment);
            }
        }
        if (PERSONFRAGMENTTAG == index) {
            if (mPersonalFragment == null) {
                mPersonalFragment = PersonalFragment.getInstance();
                transaction.add(R.id.fl_box, mPersonalFragment);
            } else {
                transaction.show(mPersonalFragment);
            }
        }
    }

    public void hideFfragment(FragmentTransaction fragmentTransaction) {
        if (mFindFragment != null) {
            fragmentTransaction.hide(mFindFragment);
        }
        if (mPersonalFragment != null) {
            fragmentTransaction.hide(mPersonalFragment);
        }
    }


}
